package com.ldq.imooc.basics.dataOperation;


import java.sql.*;

/**
 * @Author : 李道泉
 * @Description :
 * @Date : 2018/4/25 13:42
 * @Modified by :
 */
public class DBUtil {

    private static final String URL = "jdbc:mysql://127.0.0.1:3306/imooc?useSSL=false&serverTimezone=UTC";
    private static final String USER = "root";
    private static final String PASSWORD = "root";

    private static Connection conn = null;

    static {

        //1.加载驱动程序
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获得数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static Connection getConn() {
        return conn;
    }

    public static void main(String[] args) throws Exception {

        //1.加载驱动程序
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获得数据库连接
        Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
        //3、通过数据库的连接操作数据库，实现增删改查
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select t.user_name,t.age from imooc_goddess t");

        while (resultSet.next()) {

            System.out.println(resultSet.getString("user_name") + " : " + resultSet.getInt("age"));
        }
    }
}
